﻿using Configuration;
using PLC.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PLC
{
    class EnvDataManagerHL : IEnvDataManager
    {
        //int experimentType;
        //int pumpType;

        PLCManager plc = null;//= PLCManager.GetInstance();
       
        SerialPortManager serialPortManager = null;// = SerialPortManager.GetInstance();


        private void initManager()
        {
            plc = PLCManager.GetInstance(PLCConfig.IP, PLCConfig.Rack, PLCConfig.Slot, PLCConfig.ConnectionType);
            serialPortManager = SerialPortManager.GetInstance(SerialPortConfig.Port, SerialPortConfig.BaudRate, SerialPortConfig.Parity, SerialPortConfig.DataBits, SerialPortConfig.StopBits);

        }


        static EnvDataManagerHL envDataManager = null;
        private EnvDataManagerHL()
        {
            initManager();
        }
        public static EnvDataManagerHL GetInstance()
        {
            if (envDataManager == null)
            {
                envDataManager = new EnvDataManagerHL();

            }
            return envDataManager;
        }

        Boolean isConnected = false;

        public bool IsConnected { get => isConnected; }


        public void Connect()
        {
            if (plc != null)
            {
                if (!plc.IsConnected)
                {
                    int ret = plc.Connect();
                    if (ret != 0)
                    {
                        throw new Exception("PLC 连接错误，返回值是：" + ret);
                    }
                }
            }
           
            if (serialPortManager != null)
            {
                if (!serialPortManager.IsOpen)
                {
                    serialPortManager.Open();
                }
            }

            isConnected = true;
        }

        public void Close()
        {
            if (plc != null)
            {
                if (plc.IsConnected)
                {
                    plc.Close();
                }
            }

            if (serialPortManager != null)
            {
                if (serialPortManager.IsOpen)
                {
                    serialPortManager.Close();
                }
            }
            isConnected = false;
        }

        private double getValue(ushort data1, ushort data2)
        {
            byte[] b = new byte[4];
            b[0] = (byte)(data1 >> 8);
            b[1] = (byte)data1;
            b[2] = (byte)(data2 >> 8);
            b[3] = (byte)data2;
            /// byte[] byteTemp = new byte[4] { b[0], b[1], b[2], b[3] };
            byte[] byteTemp = new byte[4] { b[3], b[2], b[1], b[0] };

            return (double)BitConverter.ToSingle(byteTemp, 0);
        }
        /// <summary>
        /// 读取数据，需要传入电流变比,后续电流变比也可读
        /// </summary>
        /// <param name="currentTransformationRatio">电流变比</param>
        /// <returns></returns>
        public EnvData ReadData(int dn, double transformationRatio)
        {
            double presureIn = plc.readVData(100);
            double presureOut = plc.readVData(110);

            double flow = plc.readVData(dn);
            //plc.readVData(experiment.FlowmeterAddress);流量计地址选择，这里选择也没有做
            double temperature = plc.readVData(180);
            double speed = plc.readVData(210);
            //double speed = plc.readVData(190);

            double voltage = 0.0f;
            double current = 0.0f;
            double activePower = 0.0f;
            double powerfactor = 0.0f;

            if(serialPortManager!=null)
            {
            byte[] byteData = serialPortManager.readData();
            if (byteData != null)
            {
                voltage = BitConverter.ToSingle(new byte[] { byteData[51], byteData[52], byteData[53], byteData[54] }, 0);
                current = BitConverter.ToSingle(new byte[] { byteData[55], byteData[56], byteData[57], byteData[58] }, 0);
                activePower = BitConverter.ToSingle(new byte[] { byteData[59], byteData[60], byteData[61], byteData[62] }, 0);
            }
            }

            EnvData data = new EnvData(presureIn, presureOut, flow, temperature, speed, voltage, current, activePower, powerfactor);
            Console.WriteLine(data.ToString());

            return data;
        }

        /// <summary>
        /// 开发环境不需要管，为了测试方便
        /// </summary>
        public void NextData_Test()
        {

        }

        /// <summary>
        /// 开发环境不需要管，为了测试方便
        /// </summary>
        public void SetIndexZero()
        {

        }


    }
}

